﻿using GalaSoft.MvvmLight;
using Principle20_20_20.Model;
using System.Windows.Threading;
using System;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;


namespace Principle20_20_20.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        DispatcherTimer _timer =new DispatcherTimer();

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            _timer.Interval = new TimeSpan(0,0,1); //Repeats every 1 second
            _timer.IsEnabled = false;
            _timer.Tick += new EventHandler(_timer_Tick);
        }
        

        #region INPC Properties
        /// <summary>
        /// The <see cref="CurrentTimeSpan" /> property's name.
        /// </summary>
        public const string CurrentTimeSpanPropertyName = "CurrentTimeSpan";

        private int _currentTimeSpan = 20;

        /// <summary>
        /// Sets and gets the CurrentTimeSpan property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public int CurrentTimeSpan
        {
            get
            {
                return _currentTimeSpan;
            }

            set
            {
                if (_currentTimeSpan == value)
                {
                    return;
                }

                RaisePropertyChanging(CurrentTimeSpanPropertyName);
                _currentTimeSpan = value;
                RaisePropertyChanged(CurrentTimeSpanPropertyName);
            }
        }

        #endregion

        #region Relay Commands

        private RelayCommand _startCommand;

        /// <summary>
        /// Gets the StartCommand.
        /// </summary>
        public RelayCommand StartCommand
        {
            get
            {
                return _startCommand
                    ?? (_startCommand = new RelayCommand(
                                          () =>
                                          {
                                              ExecuteStartCommand();
                                          }));
            }
        }


        private RelayCommand _dismissCommand;

        /// <summary>
        /// Gets the GoAwayCommand.
        /// </summary>
        public RelayCommand DismissCommand
        {
            get
            {
                return _dismissCommand
                    ?? (_dismissCommand = new RelayCommand(
                                          () =>
                                          {
                                              ExecuteDismissCommand();
                                          }));
            }
        }

        private RelayCommand _quitCommand;

        /// <summary>
        /// Gets the QuitCommand.
        /// </summary>
        public RelayCommand QuitCommand
        {
            get
            {
                return _quitCommand
                    ?? (_quitCommand = new RelayCommand(
                                          () =>
                                          {
                                              ExecuteQuitCommand();
                                          }));
            }
        }

        private RelayCommand _tellMeCommand;

        /// <summary>
        /// Gets the TellMeCommand.
        /// </summary>
        public RelayCommand TellMeCommand
        {
            get
            {
                return _tellMeCommand
                    ?? (_tellMeCommand = new RelayCommand(
                                          () =>
                                          {
                                              ExecuteTellMeCommand();
                                          }));
            }
        }

        #endregion

        private void ExecuteStartCommand()
        {
            if (!_timer.IsEnabled)
            {
                this.CurrentTimeSpan = 20;
                _timer.IsEnabled = true;
            }
        }

        private void ExecuteDismissCommand()
        {
            if (_timer.IsEnabled)
            {
                this.CurrentTimeSpan = 20;
                _timer.IsEnabled = false;
            }

            Messenger.Default.Send<bool>(false, "MINIMIZE_TO_TRAY");
        }

        private void ExecuteQuitCommand()
        {
            Messenger.Default.Send<bool>(false, "QUIT_APPLICATION");
        }


        private void ExecuteTellMeCommand()
        {
            Messenger.Default.Send<bool>(false, "TELL_ME_MORE_CLICKED");            
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            if (this.CurrentTimeSpan.Equals(0))
            {
                _timer.IsEnabled = false;
                Messenger.Default.Send<bool>(false, "MINIMIZE_TO_TRAY");
                this.CurrentTimeSpan = 20;
            }
            else
                this.CurrentTimeSpan--;
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }
}